package com.jigu.core.advice;

//import com.framework.core.modules.sys.entity.ErrorMessage;
//import com.framework.core.modules.sys.service.ErrorMessageService;
import com.jigu.core.exception.BusinessException;
import com.jigu.core.exception.NotFoundException;
import com.jigu.core.utils.ResponseMessage;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Han.Sun
 */
@ControllerAdvice(annotations = {RestController.class, ResponseBody.class})
@Order(1)
public class RestControllerExceptionTranslator {


    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    ResponseMessage noHandlerFoundException(NoHandlerFoundException exception) {
        return ResponseMessage.error(exception.getMessage(), HttpStatus.NOT_FOUND.value());
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    ResponseMessage handleException(BusinessException exception, HttpServletResponse response) {
        response.setStatus(exception.getStatus());
        return ResponseMessage.error(exception.getMessage(), exception.getStatus());
    }

    @ExceptionHandler(NotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    ResponseMessage handleException(NotFoundException exception) {
        return ResponseMessage.error(exception.getMessage(), HttpStatus.NOT_FOUND.value());
    }

    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    ResponseMessage handleException(BindException exception) {
        return ResponseMessage.error(exception.getMessage(), HttpStatus.NOT_FOUND.value());
    }



    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    ResponseMessage handleException(Exception exception) {

        //TODO 优化

//        ErrorMessage errorMessages = new ErrorMessage();
//        errorMessages.setCreateDate(new Date());
//        String fullClassName = exception.getStackTrace()[0].getClassName();
//        errorMessages.setClassName(fullClassName.substring(fullClassName.lastIndexOf('.') +1,fullClassName.length()));
//        errorMessages.setMethodName(exception.getStackTrace()[0].getMethodName());
//        errorMessages.setLineNumber(exception.getStackTrace()[0].getLineNumber());
//        ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
//        exception.printStackTrace(new java.io.PrintWriter(buf, true));
//        String  stackTrace = buf.toString();
//        try {
//            buf.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        errorMessages.setStackTrace(stackTrace);
//        errorMessages.setMessage(exception.getMessage());
//        errorMessages.setStatus(0);
//        errorMessageService.asyncInsert(errorMessages);
//        exception.printStackTrace();
        return ResponseMessage.error("服务器异常，请联系管理员", 500);
    }

}
